package util

import (
	"math/rand"
	"time"

	"golang.org/x/crypto/bcrypt"
)

var (
	randStr       = []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	randNumberStr = []byte("0123456789")
)

func RandUint64() uint64 {
	now := time.Now()
	rand.Seed(now.UnixNano())
	return uint64(rand.Int63n(9007199254740992) + now.Unix())
}

func SecretPassword(pw []byte) ([]byte, error) {
	return bcrypt.GenerateFromPassword(pw, 5)
}

func CompareHashAndPassword(hashedPassword, password []byte) error {
	return bcrypt.CompareHashAndPassword(hashedPassword, password)
}

func RandString() string {
	return RandomStr(8)
}

func RandomStr(length int) string {
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < length; i++ {
		result = append(result, randStr[r.Intn(len(randStr))])
	}
	return string(result)
}

func RandomNumberStr(length int) string {
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < length; i++ {
		result = append(result, randNumberStr[r.Intn(len(randNumberStr))])
	}
	return string(result)
}
